#-*- codeing = utf-8 -*-
#@Time : 2020/11/30 16:38
#@Author : 阳某
#@File : 添加客户.py
#@Software : PyCharm
from hyrobot.common import *
from lib.webapi import apimgr



# class c1:
#     name = '添加客户API - API-0151'
#
#     # # 初始化方法
#     # def setup(self):
#     #     apimgr.mgr_login()
#     #     apimgr.order_del_all()
#     #     apimgr.customer_del_all()   #被依赖的要先删
#     #     apimgr.medicine_del_all()
#
#     # 清除方法
#     def teardown(self):
#         # 根据客户id删除
#         apimgr.customer_del(self.addedCustomerId)
#     def teststeps(self):
#
#         STEP(1,'添加一个客户')
#         r = apimgr.customer_add('南京中医药',
#                             '12346578911',
#                             '武汉是西北医院')
#         addRet = r.json()
#         self.addedCustomerId = addRet['id']
#         CHECK_POINT('返回的ret值为0',
#             addRet['ret'] == 0)
#
#         STEP(2, '检查系统数据')
#         r = apimgr.customer_list()
#         listRet = r.json()
#         expected = {
#             "ret": 0,
#             "retlist": [
#                 {
#                     "address": "武汉是西北医院",
#                     "id": addRet['id'],
#                     "name": "南京中医药",
#                     "phonenumber": "12346578911"
#                 },
#             ] ,
#             'total': 1
#         }
#         CHECK_POINT('返回的消息体数据',
#                     listRet == expected)


# 命令行执行     run --test *152
class c2:
    name = '添加客户API - API-0152'

    # 初始化方法
    def setup(self):
        # INFO('删除订单，客户，药品')
        # apimgr.mgr_login()
        # apimgr.order_del_all()
        # apimgr.customer_del_all()   #被依赖的要先删
        # apimgr.medicine_del_all()
        INFO('添加10个客户')

        #   实例化一个属性保存id后删除，这样更安全
        self.setupcustomerids = []
        for i in range(10):
            r = apimgr.customer_add(
                f'北京中医院_{i+1}',
                f'1234657891_{i+1:02d}',
                f'北京东城区_{i+1}路')
            self.setupcustomerids.append(r.json()['id'])
    # 清除方法
    def teardown(self):

        # # 因为这是在空白数据环境下，所以可以全部删除，在其他环境下不可以
        # apimgr.customer_del_all()

        for cid in self.setupcustomerids:
            apimgr.customer_del(cid)
    def teststeps(self):
        STEP(1, '先列出客户')
        r = apimgr.customer_list()
        listRet1 = r.json()
        customerlist1 = listRet1['retlist']

        STEP(2,'添加一个客户')
        r = apimgr.customer_add('北京中医院',
                            '12346578911',
                            '北京东城区')
        addRet = r.json()
        self.addedCustomerId = addRet['id']
        CHECK_POINT('返回的ret值为0',
            addRet['ret'] == 0)

        STEP(3, '再次列出客户')
        r = apimgr.customer_list(11)
        listRet = r.json()
        expected = {
            "ret": 0,
            "retlist": [
                {
                    "address": "北京东城区",
                    "id": addRet['id'],
                    "name": "北京中医院",
                    "phonenumber": "12346578911"
                }
            ] + customerlist1 ,
            'total': 11
        }
        CHECK_POINT('返回的消息体数据',
                    listRet == expected)


# class c3:
#     name = '添加客户API - API-0153'
#
#     # # 初始化方法
#     # def setup(self):
#     #     # 打开fiddle抓包分析，500是系统问题
#     #     apimgr.mgr_login(useProxy=True)
#     #     apimgr.order_del_all()
#     #     apimgr.customer_del_all()   #被依赖的要先删
#     #     apimgr.medicine_del_all()
#
#     # 因为添加失败所以没有客户
#     # # 清除方法
#     # def teardown(self):
#     #     # 根据客户id删除
#     #     apimgr.customer_del(self.addedCustomerId)
#
#     def teststeps(self):
#
#         STEP(1,'添加一个客户')
#         data = {
#             # "name": '',
#             "phonenumber": '12346578911',
#             "address": '武汉是西北医院'
#         }
#         r = apimgr.customer_add2(data)
#         addRet = r.json()
#         self.addedCustomerId = addRet['id']
#         CHECK_POINT('返回的ret',
#             addRet == {
#                 'ret':1,
#                 'msg':'请求参数错误',
#                     })
#
#         STEP(2, '检查系统数据')
#         r = apimgr.customer_list()
#         listRet = r.json()
#         expected = {
#             "ret": 0,
#             "retlist": [] ,
#             'total':0
#         }
#         CHECK_POINT('返回的消息体数据',
#                     listRet == expected)

'''
以上测试代码，做了很多重复工作，比如重复初始化工作，浪费资源
'''


